package com.best.onegous.Adapter;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.best.onegous.R;
import com.best.onegous.View.RushBuyCountDownTimerView;
import com.best.onegous.agreement.Apps;
import com.best.onegous.agreement.Common;
import com.best.onegous.details.News;
import com.best.onegous.details.YiJieXiao;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class JieXiaoAdapter extends BaseAdapter {
	Intent intent;
	private List<JSONObject> data = new ArrayList<JSONObject>();
	private LayoutInflater layoutInflater;
	private Context context;

	final int TYPE_1 = 0;
	final int TYPE_2 = 1;

	public JieXiaoAdapter(Context context, List<JSONObject> data) {

		this.context = context;
		this.data = data;
		this.layoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub

		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub

		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub

		try {
			if ("0".equals(data.get(position).getString("shop_info"))) {
				return TYPE_1;
			} else {
				return TYPE_2;
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		 return super.getItemViewType(position);
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub

		return 2;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		ViewHomes viewhomes = null;
		ViewHome viewhome = null;
		int type = getItemViewType(position);
		if (convertView == null) {
			switch (type) {
			case TYPE_1:
				viewhome = new ViewHome();
				convertView = layoutInflater.inflate(R.layout.jiexiao_item,
						null);
				viewhome.timerView = (RushBuyCountDownTimerView) convertView
						.findViewById(R.id.timerView);

				convertView.setTag(viewhome);
				break;
			case TYPE_2:
				viewhomes = new ViewHomes();
				convertView = layoutInflater.inflate(R.layout.jiexiao_items,
						null);
				viewhomes.img = (ImageView) convertView.findViewById(R.id.img);
				 viewhomes.qishu = (TextView) convertView.findViewById(R.id.qishu);
				 viewhomes.title = (TextView) convertView.findViewById(R.id.title);
				 viewhomes.name = (TextView) convertView.findViewById(R.id.name);
				 viewhomes.time = (TextView) convertView.findViewById(R.id.time);
				 viewhomes.item = (LinearLayout) convertView.findViewById(R.id.item);

				convertView.setTag(viewhomes);
				break;

			}
		} else {
			switch (type) {
			case TYPE_1:
				viewhome = (ViewHome) convertView.getTag();
				break;
			case TYPE_2:
				viewhomes = (ViewHomes) convertView.getTag();
				break;
			}
		}

		 final JSONObject item = data.get(position); 
		switch (type) {
		case TYPE_1:
			viewhome.timerView.setTime(3, 10, 9);
			viewhome.timerView.start();

			break;
		case TYPE_2:
			try {
				Apps.imageLoader.displayImage(Common.PHOTOURL+item.getString("thumb"), viewhomes.img, Apps.yuanimg);
				viewhomes.qishu.setText("第"+item.getString("qishu")+"期");
				viewhomes.title.setText(item.getString("title"));
				viewhomes.name.setText(item.getString("username"));
				viewhomes.time.setText(item.getString("q_end_time"));
				viewhomes.item.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						intent = new Intent(context, YiJieXiao.class);
						try {
							intent.putExtra("id", item.getString("id"));
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						context.startActivity(intent);
						
					}
				});
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;

		}

		return convertView;
	}

	class ViewHome {
		private LinearLayout item;
		private RushBuyCountDownTimerView timerView;
		private ImageView jiexiaoimg;
		private TextView jiexiaotitle, jiexiaojiazhi;

	}

	class ViewHomes {
		private LinearLayout item;
		private ImageView img;
		private TextView qishu, title, name, time;

	}

}